package com.androidmvc.base.network.http;

import android.support.v4.util.ArrayMap;

import com.androidmvc.base.network.http.okhttp.BaseHttpRequestCallback;
import com.androidmvc.base.network.http.okhttp.HttpRequest;
import com.androidmvc.base.network.http.okhttp.HttpRequestCallback;
import com.androidmvc.base.network.http.okhttp.Part;
import com.androidmvc.base.network.http.okhttp.RequestParams;
import com.androidmvc.base.utils.MD5Utils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import okhttp3.Headers;

/**
 * post和get请求封装工厂类
 *
 * @author jmh
 */
public class HTTPFactory {

    private static final String APPID = "56f3ef174feb7b96022c5bad";
    private static final String APPKEY = "secrect123";

    //根据参数发送所有post请求
    public static void postRequest(String url, RequestParams params, final HttpRequestCallback listener) {
        String md5;
        if (params.getFormParams().size() > 0) {
            params.addFormDataPart("appid", APPID);
            md5 = encryptionParams(params.getFormParams());
        } else {
            params.addFormDataPart("appid", APPID);
            md5 = MD5Utils.getMD5String(APPID + APPKEY);
        }
        params.addFormDataPart("appsign", md5);
        HttpRequest.post(url, params, new BaseHttpRequestCallback() {
            @Override
            protected void onSuccess(Headers headers, Object o) {
                if (listener != null) {
                    listener.onSuccess(o);
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                if (listener != null) {
                    listener.onFailure(errorCode, msg);
                }
            }

            @Override
            public void onProgress(int progress, long networkSpeed, boolean done) {
                if (listener != null) {
                    listener.onProgress(progress, networkSpeed, done);
                }
            }
        });
    }


    //根据参数发送所有get请求
    public static void getRequest(String url, RequestParams params, final HttpRequestCallback listener) {
        String md5;
        if (params.getFormParams().size() > 0) {
            params.addFormDataPart("appid", APPID);
            md5 = encryptionParams(params.getFormParams());
        } else {
            params.addFormDataPart("appid", APPID);
            md5 = MD5Utils.getMD5String(APPID + APPKEY);
        }
        params.addFormDataPart("appsign", md5);
        HttpRequest.get(url, params, new BaseHttpRequestCallback() {
            @Override
            protected void onSuccess(Headers headers, Object o) {
                if (listener != null) {
                    listener.onSuccess(o);
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                if (listener != null) {
                    listener.onFailure(errorCode, msg);
                }
            }

            @Override
            public void onProgress(int progress, long networkSpeed, boolean done) {
                if (listener != null) {
                    listener.onProgress(progress, networkSpeed, done);
                }
            }
        });
    }


    /**
     * add by jmh 2016-06-27 网络请求加密算法
     *
     * @param
     */
    private static String encryptionParams(List<Part> params) {
        String[] temp = new String[params.size()];
        StringBuffer sb = new StringBuffer();
        Map<String, String> mMap = new ArrayMap<>();
        for (int i = 0; i < params.size(); i++) {
            temp[i] = params.get(i).getKey();
            mMap.put(params.get(i).getKey(), params.get(i).getValue());
        }
        Arrays.sort(temp);
        for (int i = 0; i < temp.length; i++) {
            sb.append(mMap.get(temp[i]));
        }
        return MD5Utils.getMD5String(sb.toString() + APPKEY);
    }
}